<?php

namespace app\admin\server\music;

use app\admin\model\music\MusicSelectedPackTypeModel;
use app\admin\ServerBase;
use app\admin\validate\music\MusicSelectedPackTypeValidate;
use think\db\exception\DbException;
use think\facade\Db;
use think\Request;

class MusicSelectedPackTypeServer extends ServerBase
{
    protected $model;
    protected $request;
    /**
     * @var mixed
     */
    protected $param;
    /**
     * @var mixed
     */
    protected $validate;

    function __construct( Request $request)
    {
        //$this->checkAuth();
        $this->request=$request;
        $this->param=$request->param();
        $this->model=new MusicSelectedPackTypeModel();
        $this->validate = new MusicSelectedPackTypeValidate();
    }

    function getList(){
        [$page,$limit] = $this->getPage($this->param);
        [$list,$count] = $this->model->getList($this->request,[],'*',$page,$limit);
        $result['data']=$list;
        $result['count']=$count;
        $this->getMessageCode('adminMessage.success.select',2);
        return $result;
    }

    function getDetail(){
        $id =  $this->request->param('id');
        $info = $this->model->getFindById($id);
        if(empty($info)){
            $this->getMessageCode('adminMessage.error.select',1);
            return false;
        }
        $this->getMessageCode('adminMessage.success.select',2);
        return $info;
    }
    function add(){
        $post = $this->request->post();
        Db::startTrans();
        try {
            if(!$this->validate->scene('create')->check($post)){
                $this->ERROR = $this->validate->getError();
                return false;
            }
            $this->model->save($post);
            Db::commit();
            $this->getMessageCode('adminMessage.success.insert',2);
            return true;
        }catch (\Error $error){
            Db::rollback();
            $this->ERROR = $error->getMessage();
            return false;
        }catch (DbException $dbException){
            Db::rollback();
            $this->ERROR = $dbException->getMessage();
            return false;
        }
    }
    function edit(){
        $id = $this->request->param('id',0);
        $post = $this->request->post();
        $post['id'] = $id;
        if(!$this->validate->scene('update')->check($post)){
            $this->ERROR = $this->validate->getError();
            return false;
        }
        $info  = $this->model->getFindById($id);
        if(empty($info)){
            $this->getMessageCode('adminMessage.error.update',1);
            return false;
        }
        Db::startTrans();
        try {
            $info->save($post);
            Db::commit();
            $this->getMessageCode('adminMessage.success.update',2);
            return true;
        }catch (\Error $error){
            Db::rollback();
            $this->ERROR = $error->getMessage();
            return false;
        }catch (DbException $dbException){
            Db::rollback();
            $this->ERROR = $dbException->getMessage();
            return false;
        }
    }
}